﻿using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using SPCAM.Plugin.Cam.ViewModel;

namespace SPCAM
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 配置文件
        ///  <para>使用方法: string parameter1 = App.Configuration["AppSettings:Parameter1"];</para>
        /// 
        /// </summary>
        public static IConfiguration Configuration { get; private set; }
        public App()
        {
            Services = ConfigureServices();
            SpCad.GlobalInstance.Start();
          
            #region 配置文件
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            Configuration = builder.Build();
          //  ChangeToken.OnChange(() => Configuration.GetReloadToken(), OnConfigurationChanged);
            #endregion

            
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            SpCad.GlobalInstance.Initialize();
            base.OnStartup(e);
        }
        protected override void OnExit(ExitEventArgs e)
        {
            SpCad.GlobalInstance.Exit();
            base.OnExit(e);
        }

        /// <summary>
        /// Gets the current <see cref="App"/> instance in use
        /// </summary>
        public new static App Current => (App)Application.Current;

        /// <summary>
        /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
        /// </summary>
        public IServiceProvider Services { get; }

        /// <summary>
        /// Configures the services for the application.
        /// IFilesService filesService = App.Current.Services.GetService<IFilesService>();
        /// </summary>
        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();
            services.AddSingleton<WeldTreeViewModel>();
            return services.BuildServiceProvider();
        }
    }

}
